﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;
using Newtonsoft.Json;
using System.Web.SessionState;


namespace AjaxLibrary
{
    /// <summary>
    /// 常规商品
    /// </summary>
    class buyer_ConventionalPrice:IHttpHandler,IRequiresSessionState
    {
        #region IHttpHandler 成员

        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json";
            string nodeId = context.Request["node"];

            int companyId = 0;
            companyId = logic.customer.getCompanyId();

            bool IsCompanyType = false;

            IsCompanyType = logic.company.listCompanyWeekWithByCompanyId(companyId);

            if (!string.IsNullOrEmpty(nodeId))
            {
                if (IsCompanyType == false)
                {
                    DataTable dt = new DataTable();
                    String[] ids = nodeId.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    // 查询商品类别
                    if (ids[0] == "0")
                    {
                        int category = 0;
                        int.TryParse(ids[1], out category);

                        if (category !=0)
                        {
                            dt = logic.buyerRange.subCategory_listWithCompanyNotRange(companyId, category);
                        }
                        else
                        {
                            dt = logic.buyerRange.rootCategorylistWithCompanyNotRange(companyId);
                        }
                        using (JsonTextWriter writer = new JsonTextWriter(context.Response.Output))
                        {
                            writer.WriteStartArray();
                            foreach (DataRow row in dt.Rows)
                            {
                                writer.WriteStartObject();
                                writer.WritePropertyName("id");
                                if (row["treelevel"].ToString() == "1" && row["parentId"].ToString() != "0")
                                {
                                    writer.WriteValue("1," + row["categoryId"]);
                                }
                                else
                                {
                                    writer.WriteValue("0," + row["categoryId"]);
                                }
                                writer.WritePropertyName("text");
                                writer.WriteValue(row["name"]);
                                writer.WritePropertyName("singleClickExpand");
                                writer.WriteValue(true);
                                writer.WriteEndObject();
                            }
                            writer.WriteEndArray();
                        }
                    }
                    else
                    {
                        // 查询商品
                        dt = logic.buyerRange.listWithCategoryAndCompanyNotRange(logic.customer.getCompanyId(), Convert.ToInt32(ids[1]));

                        using (JsonTextWriter writer = new JsonTextWriter(context.Response.Output))
                        {
                            writer.WriteStartArray();
                            foreach (DataRow row in dt.Rows)
                            {
                                string names = row["name"] + "<font color='red'>" + (Convert.ToInt32(row["isCurrentCost"]) == 1 ? "时价" : String.Format("￥{0:F2}", row["newPrice"])) + "</font>";
                                writer.WriteStartObject();
                                writer.WritePropertyName("id");
                                writer.WriteValue("1" + row["id"]);
                                writer.WritePropertyName("text");
                                writer.WriteValue(names);
                                writer.WritePropertyName("leaf");
                                writer.WriteValue(true);
                                writer.WritePropertyName("href");
                                writer.WriteValue(String.Format("javascript:callbackFunction({0},{1});", row["productId"], row["businessNature"]));
                                writer.WritePropertyName("qtip");
                                writer.WriteValue(names);
                                writer.WriteEndObject();
                            }
                            writer.WriteEndArray();
                        }
                    }
                }
            }
        }

        #endregion
    }
}
